---
import sprites from '../assets/sprites.svg';
import { type TweetWithUser, formatTimestamp, replaceShortenedUrls } from '../util/tweet.js';

const tweet: TweetWithUser = replaceShortenedUrls(Astro.props.data);
---

<style>
  .text {
    color: var(--sl-color-text-accent);
  }
  .n .name,
  .engagement > * {
    margin-top: 0 !important;
  }
  header {
    display: flex;
    align-items: center;
    line-height: 1.3;
  }
  .n .profile {
    border-radius: 9999px;
    width: 48px;
    height: 48px;
  }
  .n {
    display: flex;
    gap: 1rem;
  }
  .n .name {
    display: flex;
    flex-direction: column;
  }
  .engagement {
    display: flex;
    align-items: center;
    justify-content: space-around;
    color: #595959;
  }
  .engagement > * {
    display: flex;
    gap: 0.5rem;
    white-space: nowrap;
    align-items: center;
  }
  .engagement svg {
    fill: currentColor;
  }
</style>

{
  tweet && (
    <>
      <header class="n">
        <img class="profile" src={tweet.user.profile_image_url} />
        <div class="name">
          <span class="real">{tweet.user.name}</span>
          <span class="screen">@{tweet.user.username}</span>
        </div>
      </header>
      <p class="text" set:html={tweet.text.trim().replace(/\n/g, '<br />')} />
      <p class="timestamp">
        <a
          href={`https://twitter.com/${tweet.user.username}/status/${tweet.id}`}>
          {formatTimestamp(tweet.created_at)}
        </a>
      </p>
      <footer class="engagement">
        <div>
          <svg width="24" height="24">
            <title>replies</title>
            <use href={`${sprites.src}#replies`} />
          </svg>
          <span>{tweet.public_metrics.reply_count}</span>
        </div>
        <div>
          <svg width="24" height="24">
            <title>retweets</title>
            <use href={`${sprites.src}#retweets`} />
          </svg>
          <span>
            {tweet.public_metrics.retweet_count +
              tweet.public_metrics.quote_count}
          </span>
        </div>
        <div>
          <svg width="24" height="24">
            <title>likes</title>
            <use href={`${sprites.src}#likes`} />
          </svg>
          <span>{tweet.public_metrics.like_count}</span>
        </div>
        <div>
          <svg width="24" height="24">
            <title>bookmarks</title>
            <use href={`${sprites.src}#bookmarks`} />
          </svg>
          <span>{tweet.public_metrics.bookmark_count}</span>
        </div>
      </footer>
    </>
  )
}
